﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Airport;
using System.Diagnostics;

namespace AirportConfigurationEditor.Formatters
{
    [Serializable]
    [ValueConversion(typeof(Runway), typeof(string))]
    public class RunwayNameFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Runway input = (Runway)value;

            return input.Direction1 + "/" + input.Direction2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [Serializable]
    [ValueConversion(typeof(Double), typeof(string))]
    public class DoubleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            string sourceValue = value.ToString();
            Debug.WriteLine("Convert: value.ToString() = " + value.ToString());
            Double decimalValue = 0;
            
            if (Double.TryParse(sourceValue, out decimalValue))
            {
                Debug.WriteLine("Convert: decimalValue.ToString() = " + decimalValue.ToString());
                return decimalValue;
            }
            
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string sourceValue = value.ToString();
            Debug.WriteLine("ConvertBack: value.ToString() = " + value.ToString());
            Double decimalValue = 0;

            if (Double.TryParse(sourceValue, out decimalValue))
            {
                Debug.WriteLine("ConvertBack: decimalValue.ToString() = " + decimalValue.ToString());
                return decimalValue;
            }
            return value;
        }
    }

    [Serializable]
    [ValueConversion(typeof(Node), typeof(string))]
    public class NodeUpdaterConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Node node = (Node)value;

            return node.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String nodeString = value.ToString();
            List<string> coordinateStrings = new List<string>();

            char[] separators = new char[] { ' ', ',', '(', ')' };
            // Parse Node coordinates
            coordinateStrings = new List<string>(nodeString.Split(separators, StringSplitOptions.RemoveEmptyEntries));

            int x, y;
            Int32.TryParse(coordinateStrings[0], out x);
            Int32.TryParse(coordinateStrings[1], out y);

            return new Node(x, y);
        }
    }
}
